<?php

/*
 *  公告控制器
 */

namespace Qwadmin\Controller;

class AmmountController extends ComController {
    /**
     *  公告管理
     */
    public function index(){
        $page = I("p" , 1 ,  'intval') ;
        $this->assign("data" , D('ammount')->getList(20 , [] , $page));
        $this->display();
    }
    
    /**
     * 处理表单数据
     */
    public function hundle(){
        $aid        = I("id" , 0 , 'intval');
        //内容并没有采用富文本，对换行进处理
        $content    = ntobr(htmlspecialchars(I("content") , ENT_NOQUOTES));
        $status     = I("status") == "true" ? 1 : 0 ;
        $data = [
            "title"      => htmlspecialchars(I("title") , ENT_NOQUOTES),
            "content"    => $content,
            "status"     => $status,
            'create_time'=> time()
        ];
        if($aid){
            $row = D("ammount")->edit($aid , $data);
            $row ? $this->ajaxReturn(['status' => 1 , 'msg' => '编辑公告成功']) : $this->ajaxReturn(['status' => 0 , 'msg' => '编辑公告失败']);
        }else{
            $row = D("ammount")->publish($data);
            $row ? $this->ajaxReturn(['status' => 1 , 'msg' => '添加公告成功']) : $this->ajaxReturn(['status' => 0 , 'msg' => '添加公告失败']);
        }
        
    }
    
    /**
     *  发布公告
     */
    public function add(){
        $id = I("get.id" , 0 , 'intval');
        $title = $id ? "编辑公告" : "添加公告";
        $data = D("ammount")->getModel($id);
        $this->assign("data" , $data);
        $this->display();
    }
    
    /**
     *  公告展示
     */
    public function view(){
        $aid = I("id" , 0 , 'intval');
        $data =  D("ammount")->getModel($aid);
        $this->ajaxReturn([$data]);
    }
    
    /**
     *  获取最新的公告
     */
    public function new_amount(){
        $ammount_data = D("ammount")->getList(1);
        if(empty($ammount_data['list'])){
            $this->ajaxReturn(['data' => 0]);
            
        }
        $this->ajaxReturn([
                            'data'      => $ammount_data['list'][0]['id'] , 
                            'title'     => $ammount_data['list'][0]['title'],
                            'content'   => $ammount_data['list'][0]['content'],
                        ]);
    }
    
    /**
     *  删除公告
     */
    public function del(){
        $id = I("id" , 0 , 'intval');
        $row = D("ammount")->remove($id);
        
        $row ? $this->ajaxReturn(['status' => 1 , 'msg' => '删除公告成功']) : $this->ajaxReturn(['status' => 0 , 'msg' => '删除公告失败']);
    }
    
    
}
